/*
 * Copyright 2011-2012 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

Components.utils.import("resource://oaa-cache-inspector/cache-inspector-common.js");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");


/**
 * @file sidebar-rule-summary.js
 *
 * @desc Supports the sidebar showing rule summary information for a list if cache items 
 */

/* ---------------------------------------------------------------- */
/*            OAA_CI and OAA_CI.RuleCategoryView Object             */ 
/* ---------------------------------------------------------------- */

if (typeof OAA_CI === "undefined") {
  var OAA_CI = {};
}

if (typeof OAA_CI.RuleCategoryView === "undefined") {
  OAA_CI.RuleCategoryView = {};
}


/* ---------------------------------------------------------------- */
/*       Rule Category Cache Item Views and View Controllers        */ 
/* ---------------------------------------------------------------- */

/**
 * @file sidebar-rule-category-view.js
 *
 * @desc Supports the sidebar showing rule summary information for a list if cache items 
 */

Components.utils.import("resource://oaa-cache-inspector/cache-inspector-common.js");

/* ---------------------------------------------------------------- */
/*            OAA_CI and OAA_CI.RuleResultSummary Object                  */ 
/* ---------------------------------------------------------------- */

if (typeof OAA_CI === "undefined") {
  var OAA_CI = {};
}

if (typeof OAA_CI.RuleCategoryView === "undefined") {
  OAA_CI.RuleCategoryView = {};
}

/* ---------------------------------------------------------------- */
/*                  Rule Summary View Control Functions             */ 
/* ---------------------------------------------------------------- */

/**
 * @function OAA_CI.RuleCategoryView.init
 *
 * @desc Creates references for XUL controls
 */
 
OAA_CI.RuleCategoryView.init = function () {

  var doc = window.document;

  this.nls_strings         = doc.getElementById('ID_STRINGBUNDLE_LIST');
  
  this.vbox_rule_category   = doc.getElementById('ID_VBOX_RULE_CATEGORY_VIEW');
  this.label_rule_category  = doc.getElementById('ID_LABEL_RULE_CATEGORY_NAME');
  
  this.tree_rule_results   = doc.getElementById('ID_TREE_RULE_CATEGORY_VIEW');
  this.treecol_primary     = doc.getElementById('ID_TREECOL_RULE_CATEGORY_DESCRIPTION');

  this.textbox_rule_result_label   = doc.getElementById('ID_LABEL_RULE_CATEGORY_MESSAGE');
  this.textbox_rule_result_message = doc.getElementById('ID_TEXTBOX_RULE_CATEGORY_MESSAGE');
  
  this.button_rule_info     = doc.getElementById('ID_BUTTON_SHOW_RULE_INFORMATION');
  this.button_report        = doc.getElementById('ID_BUTTON_RULE_CATEGORY_REPORT');
    
  this.rule_result_items = [];
  this.rule_result_item  = null;
  
  this.sidebar_name = "no name";
  
};

/**
 * @function OAA_CI.RuleCategoryView.hide
 *
 * @desc Hides the XUL elements of the rule results view
 */
 
OAA_CI.RuleCategoryView.hide = function () {
  this.vbox_rule_category.hidden = true;
};

/**
 * @function OAA_CI.RuleCategoryView.show
 *
 * @desc Shows the rule results summary view
 *
 * @param {Boolean}  tree  -  True if the list a tree
 *
 */
 
OAA_CI.RuleCategoryView.show = function (tree) {

  if (typeof tree !== 'boolean') tree = false;

  this.vbox_rule_category.hidden = false;
  
  if (tree) this.treecol_primary.setAttribute('primary', 'true');
  else this.treecol_primary.setAttribute('primary', 'false');
  
  var state = this.treecol_primary.getAttribute('primary')
  
//  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  PRIMARY: " + state);
  
};

/**
 * @function OAA_CI.RuleCategoryView.clear
 *
 * @desc Clears the rule results summary view with a "updating" message
 */
 
OAA_CI.RuleCategoryView.clear = function () {

  var nls_strings = this.nls_strings;

  this.visible_tree_items = [];          

  var row = { level : 0,
              container : false,
              open : false,
            
              message : nls_strings.getString('updating'),
              message_prop : 'message'
            };
  
  this.visible_tree_items.push(row);               
  
  this.setView();
  
  this.ruleButtonMessageClear();
  
  OAA_CI.RuleResultView.clear();

};

/**
 * @function OAA_CI.RuleCategoryView.update
 *
 * @desc Initializes and shows the rule results summary view
 *
 * @param {String}  sort  -  property to sort values
 */
  
OAA_CI.RuleCategoryView.update = function (sort_property, sort_direction) {

  function setTotalElementValue(id, value, style) {
  
    var node = document.getElementById(id);
    
    if (node) {
    
       node.setAttribute('value', " " + value + " ");

       if (typeof value === 'number') {
         if (value > 0) node.setAttribute('class', style);
         else node.setAttribute('class', 'zero');
       }
       else {
         node.setAttribute('class', style);       
       }

    }

  }

  function setResultCountValue(id, count, label) {
  
    var node = document.getElementById(id);
    
    if (node && (typeof count === 'number')) {
    
      if (count !== 1) node.setAttribute('value', "(" + count + " total " + label + "s)");
      else node.setAttribute('value', "(" + count + " total " + label + ")");

    }

  }


  if (typeof sort_property != 'string' || sort_property === "") sort_property = "wcag20_level";
  if (typeof sort_direction != 'number') sort_direction = 1;

  var nls_strings  = this.nls_strings;
  var tree         = this.tree_rule_results;
  
  var label_total_v  = this.label_total_violations;
  var label_total_mc = this.label_total_manual_checks;

  var prefs = OAA_WEB_ACCESSIBILITY_PREF.util.getPreferences();
  
//  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("WCAG 2.0 Level: " + prefs.wcag20_level);
  
  var RULE_GROUP = OpenAjax.a11y.RULE_GROUP;

  var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  .getInterface(Components.interfaces.nsIWebNavigation)
  .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
  .rootTreeItem
  .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  .getInterface(Components.interfaces.nsIDOMWindow);
  
  var sidebar_option = mainWindow.oaa_sidebar_option;
  var sidebar_name   = mainWindow.oaa_sidebar_name;
  var sidebar_type   = mainWindow.oaa_sidebar_type;

  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Sidebar Name: " + sidebar_name + "  Option: " + sidebar_option);

  this.label_rule_category.setAttribute('value', sidebar_name);

  this.sidebar_name = sidebar_name;

  if (!tree || !nls_strings) return;

  if (!OAA_CI.ruleset) OAA_CI.updateEvaluation();

  var filtered_rule_category = null;

  if (sidebar_type === 'category') {
    filtered_rule_category = OAA_CI.ruleset.getFilteredRuleResultsByRuleCategory(sidebar_option, sidebar_name, prefs.wcag20_level, prefs.show_results_filter_value);
    OAA_CI.RuleCategoryView.filtered_rule_category = filtered_rule_category;
    filtered_rule_category.createListOfRuleResults();
    filtered_rule_category.sortListOfRuleResults('wcag20_level', -1);  
  }
  else {
    filtered_rule_category = OAA_CI.ruleset.getFilteredRuleResultsByRuleSummary(mainWindow.oaa_sidebar_option, mainWindow.oaa_sidebar_name, prefs.wcag20_level, prefs.show_results_filter_value);
    OAA_CI.RuleCategoryView.filtered_rule_category = filtered_rule_category;
    filtered_rule_category.createListOfRuleResults();
    filtered_rule_category.sortListOfRuleResults(); // use default sorting 
  }

//  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Created list of rules: " + filtered_rule_category.rule_result_items.length);
  
  var avg_impl_level = filtered_rule_category.getNLSImplementationLevel();

  setTotalElementValue('ID_LABEL_ELEMENTS_WITH_VIOLATIONS_VALUE', filtered_rule_category.node_results_with_violations.length, 'violation');  
  setResultCountValue('ID_LABEL_TOTAL_VIOLATIONS_VALUE', filtered_rule_category.violations_count, 'violation');  

  setTotalElementValue('ID_LABEL_ELEMENTS_WITH_WARNINGS_VALUE', filtered_rule_category.node_results_with_warnings.length, 'warning');  
  setResultCountValue('ID_LABEL_TOTAL_WARNINGS_VALUE', filtered_rule_category.warnings_count, 'warning');  

  setTotalElementValue('ID_LABEL_ELEMENTS_WITH_MANUAL_CHECKS_VALUE', filtered_rule_category.node_results_with_manual_checks.length, 'manual_check');  
  setResultCountValue('ID_LABEL_TOTAL_MANUAL_CHECKS_VALUE', filtered_rule_category.manual_checks_count, 'manual check');  
    
  OAA_CI.RuleCategoryView.setView();
  
};  
  
/**
 * @function OAA_CI.RuleCategoryView.setView
 *
 * @desc Updates tree view
 */
 
OAA_CI.RuleCategoryView.setView = function () {  

//  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Update View: " + this.rule_result_items.length);

  var nls_strings  = OAA_CI.RuleCategoryView.nls_strings;
  var tree         = OAA_CI.RuleCategoryView.tree_rule_results;

  var rule_result_items = OAA_CI.RuleCategoryView.filtered_rule_category.getOpenRuleResults();
  this.rule_result_items = rule_result_items;

  var treeView = {
     visible_data : rule_result_items,
     
     rowCount : rule_result_items.length,
     
     getCellText : function(row,column) {
     
       var item      = this.visible_data[row];
       item.position = (row + 1);
       
//       OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  Cell Text: " + row + " of " + rr_items_len + ": " + column.id);

       switch (column.id) {
       
       case "ID_TREECOL_RULE_CATEGORY_DESCRIPTION":
          return item.summary;
          break;
       
       case "ID_TREECOL_RULE_CATEGORY_REQUIRED":       
         return item.required;         
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_LEVEL":
         if (item.triage) return item.wcag20_level_label + "*";
         else return item.wcag20_level_label;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_IMPLEMENTATION":
         return item.implementation_label;
         break;

       case "ID_TREECOL_RULE_CATEGORY_PASSED":
         return item.passed_label;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_WARNINGS":
         return item.warnings_label;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_VIOLATIONS":
         return item.violations_label;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_FAILURES":
         return item.failures_label;
         break;
         
       case "ID_TREECOL_RULE_CATEGORY_MANUAL_CHECKS":
         return item.manual_checks_label;
         break;
       
       default:
         break;
       }  

       return "";
     },
     setTree: function(treebox){ this.treebox = treebox; },
     isContainer: function(row) { 

       var item = this.visible_data[row];

//       OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  CONTAINER: " + item.summary + " " + item.container);

       return item.container; 
       
     },
     isContainerEmpty: function(row) { 

       var item  = this.visible_data[row];

//       OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  CONTAINER: " + item.summary + " " + item.container);

       return (typeof item.children !== 'object') || (item.children.length == 0); 
       
     },
     getParentIndex: function(row) {
        
       if (!this.isContainer(row)) {
         for (var t = row - 1; t >= 0 ; t--) {
           if (this.isContainer(t)) return t;
         }
       }
           
       return -1;
     },
     isContainerOpen: function(row) { 

       var item     = this.visible_data[row];

//       OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  OPEN: " + item.summary + " " + item.open);

       return item.open; 
       
     },
     getLevel: function(row) { 

       var item     = this.visible_data[row];

//       OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  LEVEL: " + item.summary + " " + item.level);
       
       return item.level; 
       
     },
     toggleOpenState: function(row){ 

       var item = this.visible_data[row];

       if (!item.container) return;

       if (item.open) {
         item.open = false;
 
         var thisLevel = this.getLevel(row);
         var deletecount = 0;
         for (var t = row + 1; t < this.visible_data.length; t++) {
           if (this.getLevel(t) > thisLevel) deletecount++;
           else break;
         }

         if (deletecount) {
           this.visible_data.splice(row + 1, deletecount);
           this.treebox.rowCountChanged(row + 1, -deletecount);
         }
       }
       else {
         item.open = true;
      
         var children_len = item.children.length;

         for (var i = 0; i < children_len; i++) {
           this.visible_data.splice(row + i + 1, 0, item.children[i]);
         }
      
         this.treebox.rowCountChanged(row + 1, children_len);
       }
         
      this.treebox.invalidateRow(row);
  
    },
     isSeparator: function(row){ return false; },
     isSorted: function(){ return false; },
     getImageSrc: function(row,col){ return null; },
     getRowProperties: function(row,props){

       var item     = this.visible_data[row];
       
       property_value = "";

       if (item.triage) property_value = "triage";

       if (property_value != ""){
         var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
         props.AppendElement(aserv.getAtom(property_value));
       }

     },
     cycleHeader : function(col, elem) {},
     getCellProperties: function(row,column,props){

       var item     = this.visible_data[row];
       
       property_value = "";

       switch (column.id) {
              
       case "ID_TREECOL_RULE_CATEGORY_DESCRIPTION":
         property_value = item.summary_prop;
          break;
       
       case "ID_TREECOL_RULE_CATEGORY_REQUIRED":       
         property_value = item.required_prop;
         break;

       case "ID_TREECOL_RULE_CATEGORY_LEVEL":       
         property_value = item.wcag20_level_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_IMPLEMENTATION":
         property_value = item.implementation_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_PASSED":
         property_value = item.passed_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_WARNINGS":
         property_value = item.warnings_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_VIOLATIONS":
         property_value = item.violations_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_FAILURES":
         property_value = item.failures_prop;
         break;
       
       case "ID_TREECOL_RULE_CATEGORY_MANUAL_CHECKS":
         property_value =  item.manual_checks_prop;
         break;
       
       default:
         break;
       }  

       if (property_value != ""){
         var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
         props.AppendElement(aserv.getAtom(property_value));
       }
     
     },
     getColumnProperties: function(colid,col,props){}
   }
   
  tree.view = treeView;
 
  this.rule_result_item = null;    
  this.ruleButtonMessageClear();
  OAA_CI.RuleResultView.clear();
  
};

/**
 * @function OAA_CI.RuleCategoryView.sortRuleList
 *
 * @desc Sorts the list of rules by a sort property
 */
 
OAA_CI.RuleCategoryView.sortRuleList = function (column) {
  
  var tree = this.tree_rule_results;

  var column_name;
  var order = tree.getAttribute("sortDirection") == "ascending" ? 1 : -1;
  
  //if the column is passed and it's already sorted by that column, reverse sort
  if (column) {
    column_name = column.id;

    if (tree.getAttribute("sortResource") == column_name) {
      order *= -1;
    }
  } else {
    column_name = tree.getAttribute("sortResource");
  }

  //setting these will make the sort option persist
  tree.setAttribute("sortDirection", order == 1 ? "ascending" : "descending");
  tree.setAttribute("sortResource", column_name);
  
  //set the appropriate attributes to show to indicator
  var cols = tree.getElementsByTagName("treecol");

  for (var i = 0; i < cols.length; i++) {
    cols[i].removeAttribute("sortDirection");
  }

  document.getElementById(column_name).setAttribute("sortDirection", order == 1 ? "ascending" : "descending");

  var frc = OAA_CI.RuleCategoryView.filtered_rule_category;

  switch (column.id) {
       
  case "ID_TREECOL_RULE_CATEGORY_DESCRIPTION":
    frc.sortListOfRuleResults('summary', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_REQUIRED":       
    frc.sortListOfRuleResults('rule_type', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_LEVEL":
    frc.sortListOfRuleResults('wcag20_level', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_IMPLEMENTATION":
    frc.sortListOfRuleResults('implementation_percentage', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_PASSED":
    frc.sortListOfRuleResults('passed_count', order);
    break;

  case "ID_TREECOL_RULE_CATEGORY_WARNINGS":
    frc.sortListOfRuleResults('warnings_count', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_VIOLATIONS":
    frc.sortListOfRuleResults('violations_count', order);
    break;

  case "ID_TREECOL_RULE_CATEGORY_FAILURES":
    frc.sortListOfRuleResults('failures_count', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_MANUAL_CHECKS":
    frc.sortListOfRuleResults('manual_checks_count', order);
    break;
       
  case "ID_TREECOL_RULE_CATEGORY_HIDDEN":
    frc.sortListOfRuleResults('hidden_count', order);
    break;
       
  default:
    break;
  }  
  
  OAA_CI.RuleCategoryView.setView();

};


/**
 * @function OAA_CI.RuleCategoryView.onSelect
 *
 * @desc Updates node results when a cache item is selected
 */
 
OAA_CI.RuleCategoryView.onSelect = function () {

  var tree_rule_results = OAA_CI.RuleCategoryView.tree_rule_results;
  
  OAA_CI.RuleCategoryView.rule_result_item = null;

  if (tree_rule_results) {
  
    if (OAA_CI.RuleCategoryView.filtered_rule_category.rule_result_items) {
    
      var index = this.tree_rule_results.currentIndex;
    
      if (index >= 0) {

//        OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("  Rule Result Select Index: " + index + " of " + OAA_CI.RuleCategoryView.filtered_rule_category.filtered_rule_results.length);

        var rule_result_item = OAA_CI.RuleCategoryView.rule_result_items[index].filtered_rule_result;
      
        OAA_CI.RuleCategoryView.rule_result_item = rule_result_item;
 
//        OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Filtered Rule Result Item: " + OAA_CI.RuleCategoryView.rule_result_item.rule_result.getRuleSummary() + " " + OAA_CI.RuleCategoryView.rule_result_item.filtered_node_results.length);  

        if (rule_result_item && rule_result_item.rule_result) {
          OAA_CI.RuleResultView.update(rule_result_item, (index + 1));   
          OAA_CI.RuleCategoryView.ruleButtonUpdate(rule_result_item, (index + 1));
        }
        else {
          OAA_CI.RuleResultView.clear();
          OAA_CI.RuleCategoryView.ruleButtonMessageClear();  
        }
      }  
      else {
        OAA_CI.RuleResultView.clear();
        OAA_CI.RuleCategoryView.ruleButtonMessageClear();  
      }
    }
    else {
      OAA_CI.RuleResultView.clear();
      OAA_CI.RuleCategoryView.ruleButtonMessageClear();  
    }
  } 
};

/**
 * @function OAA_CI.RuleCategoryView.showRuleInformation
 *
 * @desc Show information about the currently selected rule 
 */
 
OAA_CI.RuleCategoryView.showRuleInformation = function () {

  if (this.rule_result_item && this.rule_result_item.rule_result) { 
    OAA_CI.showRulePropertiesDialog(this.rule_result_item.rule_result);
  }
};



/* ---------------------------------------------------------------- */
/*        Rule Result Information Button and Message                */ 
/* ---------------------------------------------------------------- */

/**
 * @function OAA_CI.RuleCategoryView.ruleButtonMessageClear
 *
 * @desc Clears the rules in the rules tabs
 */
 
OAA_CI.RuleCategoryView.ruleButtonMessageClear = function () {

  if (this.nls_strings && this.button_rule_info) {
    this.button_rule_info.setAttribute("label", this.nls_strings.getString('rule') + ' ' + this.nls_strings.getString('information'));
    this.button_rule_info.setAttribute('tooltiptext', this.nls_strings.getString('noRuleSelected'));
    this.button_rule_info.setAttribute("disabled",    "true");
  }
  
  if (OAA_CI.rule_properties_dialog && 
      OAA_CI.rule_properties_dialog.rule_properties) {
    OAA_CI.rule_properties_dialog.rule_properties.clear();
  }

  if (this.textbox_rule_result_message) {
    this.textbox_rule_result_label.setAttribute('value', this.nls_strings.getString('rule') + " " + this.nls_strings.getString('resultMessage'));
    this.textbox_rule_result_message.setAttribute('value', this.nls_strings.getString('noRuleSelected'));
  }

  this.rule_result_item = null;

};

/**
 * @function OAA_CI.RuleCategoryView.ruleButtonUpdate
 *
 * @desc Updates the label of the show rule information button
 *
 * @param {FilteredRuleResult}  rule_result_item  - filtered rule result object   
 */
 
OAA_CI.RuleCategoryView.ruleButtonUpdate = function (rule_result_item, position) {

//  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Update button: " + rule_result_item.rule_result.getRule() + " (" + position + ")" );

  var tooltiptext;

  var nls_strings      = OAA_CI.RuleCategoryView.nls_strings;
  var button_rule_info = OAA_CI.RuleCategoryView.button_rule_info;

  if (nls_strings && button_rule_info) {
  
    var rule_result = rule_result_item.rule_result;

//    OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("   Rule Result: " + rule_result.getRuleSummary());

    if (rule_result.getRuleSummary) {
    
      tooltiptext = nls_strings.getString('moreInformationRule') + ": " + rule_result.getRuleSummary();

      button_rule_info.setAttribute("label", this.nls_strings.getString('rule') + ' ' + position + ' ' + this.nls_strings.getString('information'));
      button_rule_info.setAttribute("tooltiptext",  tooltiptext);
      button_rule_info.setAttribute("disabled", "false");

      if (this.textbox_rule_result_message) {
        this.textbox_rule_result_label.setAttribute('value', this.nls_strings.getString('rule') + " " + position + " " + this.nls_strings.getString('resultMessage'));
        this.textbox_rule_result_message.setAttribute('value', rule_result_item.rule_result.getMessage());
      }  

      if (OAA_CI.rule_properties_dialog && 
          OAA_CI.rule_properties_dialog.rule_properties) {
        OAA_CI.rule_properties_dialog.rule_properties.update(rule_result);
      }

    }

else {

      tooltiptext = nls_strings.getString('noRuleSelected');

      button_rule_info.setAttribute("label", nls_strings.getString('rule') + ' ' + nls_strings.getString('information'));
      button_rule_info.setAttribute("tooltiptext",  tooltiptext);
      button_rule_info.setAttribute("disabled", "true");
      
      if (OAA_CI.rule_properties_dialog && 
          OAA_CI.rule_properties_dialog.rule_properties) {
        OAA_CI.rule_properties_dialog.rule_properties.clear();
      }

    }
  
  }
};


/**
 * @function OAA_CI.RuleCategoryView.showRuleCategoryReport 
 *
 * @desc Opens a new tab in the window to display an HTML view of the rule category report
 */
 
OAA_CI.RuleCategoryView.showRuleCategoryReport = function () {

  if (!this.filtered_rule_category) return;

  // create HTML report file

  var dir = FileUtils.getDir('TmpD', [], true, true);

  var file_html = FileUtils.getFile('TmpD', ['report_rule_category.html']);
  
  var fileStream = FileUtils.openSafeFileOutputStream(file_html, 0x02 | 0x08 | 0x20, 0644, 0);  
  
  var html = this.filtered_rule_category.toHTML(this.sidebar_name);
  
  fileStream.write(html, html.length);
  
  FileUtils.closeSafeFileOutputStream(fileStream);
    
  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Report Rule Category URL: " + file_html.path);

  // create CSV file

  file_csv = FileUtils.getFile('TmpD', ['report_rule_category.csv']);
  
  fileStream = FileUtils.openSafeFileOutputStream(file_csv, 0x02 | 0x08 | 0x20, 0644, 0);  
  
  var csv = this.filtered_rule_category.toCSV(this.sidebar_name);
  
  fileStream.write(csv, csv.length);
  
  FileUtils.closeSafeFileOutputStream(fileStream);

  OAA_WEB_ACCESSIBILITY_LOGGING.logger.log.debug("Report Rule Category URL: " + file_csv.path);

  var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIWebNavigation)
    .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
    .rootTreeItem
    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    .getInterface(Components.interfaces.nsIDOMWindow);

  mainWindow.gBrowser.addTab(file_html.path);


};


